package scene
{
	import flash.display.BitmapData;
	import flash.display.Loader;
	import flash.display.LoaderInfo;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Matrix;
	import flash.net.URLRequest;
	import flash.text.TextField;
	
	import player.Player;  
	 
	public class Scene extends Sprite
	{
		private var m_sceneLoader:Loader;
		private var m_sceneBackgroundLoader:Loader;
		private var m_sceneForegroundLoader:Loader;
		private var m_sceneBitmapData:BitmapData = null;
		private var m_sceneBackgroundBitmapData:BitmapData = null;
		private var m_sceneForegroundBitmapData:BitmapData = null;
		
		private var m_width:int;
		private var m_height:int;
		private var m_player:Player;
		private var m_position:TextField;
		
		public function Scene()
		{  
			m_sceneBackgroundLoader = new Loader;
			m_sceneBackgroundLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, sceneBackgroundLoadComplete);
			m_sceneBackgroundLoader.load(new URLRequest("../resource/map/aolai_bg.jpg"));
			  
			m_sceneLoader = new Loader; 
			m_sceneLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, sceneLoadComplete);
			m_sceneLoader.load(new URLRequest("../resource/map/aolai.png"));
			  
			m_sceneForegroundLoader = new Loader;
			m_sceneForegroundLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, sceneForegroundLoadComplete); 
			m_sceneForegroundLoader.load(new URLRequest("../resource/map/aolai_fg.png"));
			   
			addEventListener(MouseEvent.MOUSE_DOWN, mouseDown);
			addEventListener(MouseEvent.MOUSE_UP, mouseReleased);
			addEventListener(MouseEvent.MOUSE_MOVE, mouseMove);
			addEventListener(Event.ENTER_FRAME, enterFrame);
			
			m_player = new Player();
			addChild(m_player);
			
			m_position = new TextField();
			m_position.maxChars = 32;
			m_position.textColor = 0xFF0000;
			m_position.x = 0;
			m_position.y = 0;
			
			m_position.visible = true;
			m_position.text = "position";
			addChild(m_position);
		}
		
		private function sceneLoadComplete(event:Event):void
		{
			m_sceneBitmapData = event.target.content.bitmapData;  
			
			m_sceneLoader.contentLoaderInfo.removeEventListener(Event.COMPLETE, sceneLoadComplete);
			m_sceneLoader = null; 
		}
		
		private function sceneBackgroundLoadComplete(event:Event):void
		{
			m_sceneBackgroundBitmapData = event.target.content.bitmapData;  
			
			m_sceneBackgroundLoader.contentLoaderInfo.removeEventListener(Event.COMPLETE, sceneBackgroundLoadComplete);
			m_sceneBackgroundLoader = null; 
		}
		
		private function sceneForegroundLoadComplete(event:Event):void
		{
			m_sceneForegroundBitmapData = event.target.content.bitmapData;  
			
			m_sceneForegroundLoader.contentLoaderInfo.removeEventListener(Event.COMPLETE, sceneForegroundLoadComplete);
			m_sceneForegroundLoader = null; 
		}
		
		private function enterFrame(event:Event):void
		{
			if (m_sceneBitmapData && m_sceneBackgroundBitmapData && m_sceneForegroundBitmapData)
			{
				graphics.beginBitmapFill(m_sceneBackgroundBitmapData, null, false, true);
				graphics.drawRect(0, 0, m_sceneBackgroundBitmapData.width, m_sceneBackgroundBitmapData.height); 
				
				graphics.beginBitmapFill(m_sceneBitmapData, null, false, true);
				graphics.drawRect(0, 0, m_sceneBitmapData.width, m_sceneBitmapData.height);
				
				graphics.beginBitmapFill(m_sceneForegroundBitmapData, null, false, true);
				graphics.drawRect(0, 0, m_sceneForegroundBitmapData.width, m_sceneForegroundBitmapData.height);
				
				m_sceneBitmapData = null;
				m_sceneBackgroundBitmapData = null;
				m_sceneForegroundBitmapData = null;
			}
		}
		
		private function mouseDown(event:MouseEvent):void
		{
			trace("mouse down");
			startDrag();
		}
		
		private function mouseReleased(event:MouseEvent):void
		{
			trace("mouse up"); 
			stopDrag();
		}
		
		private function mouseMove(event:MouseEvent):void
		{
			var text:String = "(" + stage.stageWidth + "," + stage.stageHeight + ")" 
				              + "(" + event.stageX + "," + event.stageY + ")";
			m_position.text = text; 
		}
	}
}